package com.example.mvvmtemplate.page.start;

import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;

import com.example.mvvmtemplate.base.BaseActivity;
import com.example.mvvmtemplate.page.login.LoginActivity;
import com.example.mvvmtemplate.R;
import com.example.mvvmtemplate.utli.permission.InvisibleFragment;
import com.example.mvvmtemplate.utli.permission.JavaPermissionX;
import com.example.mvvmtemplate.utli.permission.PermissionsDialog;

import java.util.List;
import java.util.Map;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;

public class StartActivity extends BaseActivity {

    private ImageView ivLogo;

    @Override
    public int getLayoutResID() {
        return R.layout.activity_start;
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivLogo = findViewById(R.id.iv_logo);

        registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                for (Map.Entry<String, Boolean> entry : result.entrySet()) {
                    if (!entry.getValue()) {
                        PermissionsDialog permissionsDialog = PermissionsDialog.getInstance();
                        permissionsDialog.show(getSupportFragmentManager(), "permissionsDialog");
                        return;
                    }
                }
                ivLogo.setVisibility(View.VISIBLE);
                startAnimator();
            }
        }).launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE});
    }

    @Override
    protected void onStart() {
        super.onStart();
        //request();
    }

    /**
     * 检查权限
     */
    private void request() {
        JavaPermissionX.getInstance().request(this, new InvisibleFragment.PermissionCallback() {
            @Override
            public void onPermissionCallback(boolean allGranted, List<String> deniedList) {
                if (allGranted) {
                    ivLogo.setVisibility(View.VISIBLE);
                    startAnimator();
                } else {
                    PermissionsDialog permissionsDialog = PermissionsDialog.getInstance();
                    permissionsDialog.show(getSupportFragmentManager(), "permissionsDialog");
                }
            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE);
    }

    /**
     * 授权成功后的操作
     */
    private void startAnimator() {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(ivLogo, "alpha", 0, 1);
        alpha.setDuration(1500);
        alpha.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                startActivity(LoginActivity.class);
                alpha.removeAllListeners();
                finish();
            }
        });
        alpha.start();
    }
}
